package com.example.demo.controller;

import com.example.demo.domain.Category;
import com.example.demo.domain.Item;
import com.example.demo.domain.Product;
import com.example.demo.services.CatalogService;
import com.example.demo.services.impl.NationalPolicyResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@Controller
@RestController
@CrossOrigin
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @Autowired
    private Category category;

    @Autowired
    private Product product;

    @Autowired
    private Item item;

    @Autowired
    private NationalPolicyResult nationalPolicyResult;

    @RequestMapping(value = "/categories",method = RequestMethod.GET)
    public NationalPolicyResult getCategoryList() {
        List<Category> categoryList = catalogService.getCategoryList();
        NationalPolicyResult result = nationalPolicyResult.build(2,"类别查询成功！",categoryList);
        return result;
    }

    @RequestMapping(value = "/categories/{categoryID}",method = RequestMethod.GET)
    public NationalPolicyResult getCategoryByName(@PathVariable String categoryID){
        Category category1 = catalogService.getCategory(categoryID);
        if(category1 != null){
            NationalPolicyResult result = nationalPolicyResult.build(2, "类别查询成功！", category1);
            return result;
        }
        else {
            NationalPolicyResult result = nationalPolicyResult.build(2,"未查到该类别！",null);
            return result;
        }
    }

    @RequestMapping(value = "/products",method = RequestMethod.GET)
    public NationalPolicyResult getAllProducts(){
        List<Product> resultList = catalogService.getProductList();
        NationalPolicyResult result = nationalPolicyResult.build(2,"商品清单查询",resultList);
        return result;
    }

    @RequestMapping(value = "/categories/{categoryID}/products", method = RequestMethod.GET)
    public NationalPolicyResult getProductList(@PathVariable String categoryID){
        List<Product> productList = catalogService.getProductListByCategory(categoryID);
        if(productList != null){
            NationalPolicyResult result = nationalPolicyResult.build(2,"商品查询成功！",productList);
            return result;
        }
        else {
            NationalPolicyResult result = nationalPolicyResult.build(2,"未查询到该类别商品！",null);
            return result;
        }
    }

    @RequestMapping(value = "/categories/{category}/products/{productID}", method = RequestMethod.GET)
    public NationalPolicyResult getProductByName(@PathVariable String productID){
        Product product1 = catalogService.getProduct(productID);
        if(product1 != null){
            NationalPolicyResult result = nationalPolicyResult.build(2,"商品查询成功！",product1);
            return result;
        }
        else {
            NationalPolicyResult result = nationalPolicyResult.build(2,"未查询到商品！",null);
            return result;
        }
    }

    @RequestMapping(value = "/categories/{category}/products/{product}",method = RequestMethod.POST)
    public NationalPolicyResult searchProduct(@PathVariable String product){
        List<Product> productList = catalogService.searchProductList(product);
        if(productList != null){
            NationalPolicyResult result = nationalPolicyResult.build(2,"查询成功！",productList);
            return result;
        }
        else {
            NationalPolicyResult result = nationalPolicyResult.build(2,"未查询到对应商品！",null);
            return result;
        }
    }

    @RequestMapping(value = "/categories/{category}/products/{productID}/items", method = RequestMethod.GET)
    public NationalPolicyResult getItemList(@PathVariable String productID){
        List<Item> itemList = catalogService.getItemListByProduct(productID);
        if(itemList != null){
            NationalPolicyResult result = nationalPolicyResult.build(2,"物品查询成功！",itemList);
            return result;
        }
        else {
            NationalPolicyResult result = nationalPolicyResult.build(2,"未查询到物品！",null);
            return result;
        }
    }

    @RequestMapping(value = "/categories/{category}/products/{productID}/items/{itemID}", method = RequestMethod.GET)
    public NationalPolicyResult getItemByID(@PathVariable String itemID){
        Item item1 = catalogService.getItem(itemID);
        if(item1 != null){
            NationalPolicyResult result = nationalPolicyResult.build(2,"物品查询成功！",item1);
            return result;
        }
        else {
            NationalPolicyResult result = nationalPolicyResult.build(2,"未查询到物品！",null);
            return result;
        }
    }
}
